<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title></title>
    <meta name="keywords" content="" />
    <meta name="description" content="">
    <link href="css/bulma.min.css" rel="stylesheet">
    <link href="css/github-markdown.min.css" rel="stylesheet">
    <link rel="stylesheet" href="/css/app.css">
    <script src="js/jquery.min.js"></script>
    <script src="js/config.js"></script>
    <script src="js/urlutil.js"></script>
    <script src="js/loginutil.js"></script>
</head>



<body>
    <nav id="navbar" class="navbar has-shadow">
        <div class="container">
            <div class="navbar-brand">
                <div class="navbar-burger burger" data-target="navMenu">
                    <span></span>
                    <span></span>
                    <span></span>
                </div>
            </div>
            <div id="navMenu" class="navbar-menu">
                <div class="navbar-start">
                    <a class="navbar-item" href="index.html">主页</a>
                    <a class="navbar-item" href="about.html">关于</a>
                </div>
                <div class="navbar-end">
                    <div id="login_div" style="display: none;" class="navbar-item">
                        <a id="category_mangager" style="display: none;background-color: #315790;"
                            class="button is-small is-primary" href="category.html">分类管理</a>&nbsp;&nbsp;&nbsp;
                        <a class="button is-small is-primary" style="background-color: #315790;"
                            href="add-article.html">写文章</a>&nbsp;&nbsp;&nbsp;&nbsp;
                        <a id="login_username" href="user-articlelist.html" style="color: #373737"></a>&nbsp;&nbsp;&nbsp;&nbsp;
                        <a href="javascript:logout();" style="color: #373737">退出</a>
                    </div>

                    <div id="unlogin_div" class="navbar-item">
                        <a class="button is-small is-primary" style="background-color: #315790;"
                            href="register.html">注册</a>&nbsp;&nbsp;&nbsp;&nbsp;
                        <a class="button is-small" href="login.html">登录</a>
                    </div>
                </div>
            </div>
        </div>
    </nav>

    <script>
        jQuery(function () {
            var login_username = window.localStorage.getItem("username");
            if (login_username == null) {
                // 未登录
                jQuery("#unlogin_div").show();
            } else {
                // 已经登录
                jQuery("#unlogin_div").hide();
                jQuery("#login_username").html(login_username);
                jQuery("#login_username").attr("href", "user-articlelist.html?uid=" + window.localStorage.getItem("uid"));
                jQuery("#login_div").show();
                // 管理员权限判定
                if (window.localStorage.getItem("manager") == 1) {
                    jQuery("#category_mangager").show();    // 让文章管理按钮显示
                }
            }
        });
    </script>

    <section id="hero" class="hero is-primary is-bold">
        <div class="hero-body">
            <div class="container">
                <a href="about.html">
                    <h1 class="title">技术博客</h1>
                </a>
                <h2 class="subtitle">
                </h2>
            </div>
        </div>
    </section>
    <div class="container margin-top" id="">
        <div class="columns">
            <div class="column is-8 is-offset-2">
                <div id="articleList">

                </div>

                <nav class="pagination is-small margin-top" role="navigation" aria-label="pagination">
                    <a href="javascript:doFirst();" class="pagination-previous">首页</a>
                    <a href="javascript:doBefore();" class="pagination-previous">上一页</a>
                    <a href="javascript:doNext();" class="pagination-next">下一页</a>
                    <a href="javascript:doLast();" class="pagination-next">末页</a>
                    <ul class="pagination-list" id="totalMsg">

                    </ul>
                </nav>
                <p style="display: none;" class="has-text-centered margin-top is-size-7 has-text-grey">没有更多了... </p>
            </div>
        </div>
    </div>

    <style>
        .card {
            border-radius: 4px;
            border: 1px solid #ebeef5;
            background-color: #fff;
            overflow: hidden;
            color: #303133;
            transition: .25s;
            margin: 10px 0;
            box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
            padding: 20px;
        }

        .card a:hover {
            color: #09305b;
            transition: .2s;
        }

        .card:hover {
            transform: scale(1.1);
        }


        #hero {
            /*background-image: linear-gradient(141deg,#330867 100%,#30cfd0 0%);*/
            background-image: linear-gradient(141deg, #330867 0%, #30cfd0 100%);
        }
    </style>

    <script>
        var pindex = 1; // 当前页码, 默认为 1 
        var pages = 1;  // 总共有多少页, 默认为 1

        // 尝试获取参数中的页码
        var pindex_param = getUrlParam("pindex");
        if (pindex_param != null && pindex_param > 1) {
            pindex = pindex_param;
        }

        // ajax 请求方法
        function doAjax(index) {
            jQuery.ajax({
                url: req_base_url + "article/getListByPage",
                method: "GET",
                data: {
                    "pageIndex": index
                },
                success: function (res) {
                    if (res != null && res.code == 200) {
                        // 成功
                        var listHtml = "";
                        for (var i = 0; i < res.data.records.length; i++) {
                            var article = res.data.records[i];
                            listHtml += '<div class="card">';
                            listHtml += '<div class="card-content">';
                            listHtml += '<div class="content">';
                            listHtml += '<a href="detail.html?aid=' + article.aid + '" style="color: #3b3b3b;font-weight: bold;">' + article.title + '</a>';
                            listHtml += '</div>';
                            listHtml += '</div>';
                            listHtml += '</div>';
                        }
                        jQuery("#articleList").html(listHtml);
                        // 初始化分页数据
                        jQuery("#totalMsg").html("当前第: " + res.data.current + " 页, 共: " + res.data.pages + " 页");
                        pages = res.data.pages;
                    } else {
                        // 登录失败
                        location.href = "error.html";
                        alert("抱歉: 初始化失败! " + res.msg)
                    }

                }
            })
        }

        // 去首页
        function doFirst() {
            var current_pindex = parseInt(pindex);
            if (current_pindex <= 1) {
                // 已经在首页了
                alert("已经是首页了哦");
                return false;
            }
            location.href = "index.html?pindex=1";
        }

        // 去上一页
        function doBefore() {
            var current_pindex = parseInt(pindex);
            if (current_pindex <= 1) {
                alert("前面没了~");
                return false;
            }
            current_pindex = current_pindex - 1;
            location.href = "index.html?pindex=" + current_pindex;
        }

        // 去下一页
        function doNext() {
            var current_pindex = parseInt(pindex);
            if (current_pindex >= pages) {
                alert("后面没了~");
                return false;
            }
            current_pindex = current_pindex + 1;
            location.href = "index.html?pindex=" + current_pindex;
        }

        // 去末页
        function doLast() {
            var current_pindex = parseInt(pindex);
            if (current_pindex >= pages) {
                // 已经在末页了
                alert("已经是末页了哦");
                return false;
            }
            location.href = "index.html?pindex=" + pages;
        }



        // 初始化页面数据
        function initPage() {
            doAjax(pindex);
        }
        initPage();
    </script>

</body>

</html>